package com.tedu.bill.demo7.demo6;

/**
 * Author：毕龙
 * Date: 2024/5/25 15:34
 */
public class MyThread extends Thread {

    private static Object objA = new Object();
    private static Object objB = new Object();

    @Override
    public void run() {

        while (true){
            if("线程1".equals(getName())){
                synchronized (objA){
                    System.out.println("线程1拿到筷子准备喝汤");
                    synchronized (objB){
                        System.out.println("线程1拿到勺子喝汤");
                    }
                }
            }else{
                synchronized (objB){
                    System.out.println("线程2拿到勺子喝汤");
                    synchronized (objA){
                        System.out.println("线程2拿到筷子准备喝汤");
                    }
                }
            }
        }
    }
}
